using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Projectile : MonoBehaviour
{
    Rigidbody2D rigidbody2D;
    // 声明一个 2d 游戏对象
    // Start is called before the first frame update
    void Awake()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
        // 获取当前2d刚体对象
    }

    private void Update()
    {
        if (transform.position.magnitude >= 100.0f)
        {
            Destroy(gameObject);
        }
    }
    public void Launch( Vector2 direction, float force)    // 飞弹发射脚本
    {
        rigidbody2D.AddForce(direction*force);
        // 调用 2d 刚体的 addforce 方法
        // 施加一个力，使其运动
    }

    public void OnCollisionEnter2D(Collision2D collision)
    {
        EnemyController enemyController = collision.collider.GetComponent<EnemyController>();
        // 获取 EnemController 对象的组件方法
        if (enemyController != null)
        {
            enemyController.Fix();
        }
        Debug.Log($"hit {collision.gameObject}");
        // 输出碰撞到的物体
        Destroy(gameObject);
        // 碰撞发生后飞弹消失（销毁）
    }


}
